package com.kalix.dataPlatform.right.biz;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.dataPlatform.right.api.biz.INetworkDeviceMemoryBeanService;
import com.kalix.dataPlatform.right.api.dao.INetworkDeviceMemoryBeanDao;
import com.kalix.dataPlatform.right.api.model.PostRequestUtil;
import com.kalix.dataPlatform.right.entities.NetworkDeviceMemoryBean;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONArray;
import org.json.JSONObject;

public class NetworkDeviceMemoryBeanServiceImpl extends GenericBizServiceImpl<INetworkDeviceMemoryBeanDao, NetworkDeviceMemoryBean> implements INetworkDeviceMemoryBeanService {
    public static String Ip = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String Port =(String) ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    @Override
    public String getNetworkDeviceMemoryValue() {
        String  strJson = "{" +
                "\"metricses\":[" +
                "{" +
                "\"metricsDefPath\":\"MetricsDefinition.mo=\\\"NetworkDevice\\\",name=\\\"MEM\\\"\"," +
                "\"arguments\":\"\"," +
                "\"sortable\":true" +
                "}" +
                "]," +
                "\"filters\":[" +
                "{" +
                "\"grades\":[" +
                "1," +
                "2," +
                "3" +
                "]," +
                "\"pdmEngine\":\"defaultEngine\"," +
                "\"moType\":\"NetworkDevice\"," +
                "\"moFilter\":\"1=1\"," +
                "\"permissionMoType\":\"NetworkDevice\"," +
                "\"permissionMoFilter\":\"1=1\"" +
                "}" +
                "]" +
                "}";
        String json = PostRequestUtil.HttpPostWithJson("http://"+Ip+":"+Port+"/v2/metrics/topN?top=5&role=admin",strJson);
        JSONObject jsonObject = new JSONObject(json);
        JSONArray body = jsonObject.getJSONArray("body");
        String str ="{" ;
        for(int i=0;i<body.length();i++){
            JSONObject bobject = body.getJSONObject(i);
            JSONObject mo = bobject.getJSONObject("mo");
            String name = mo.getString("name");
            JSONArray topNMetricsList = bobject.getJSONArray("topNMetricsList");
            String value = topNMetricsList.getJSONObject(0).getString("value");
            str+="\"gauge_"+(i+1)+"\":[{" +
                    "\"value\": "+value+"," +
                    "\"name\":\""+name+"\"" +
                    "}],";
        }
        str+="}";
        return str;
    }
}
